﻿using EMoney.IStock.Client.Models.HD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EMoney.IStock.Client.DataAccess.HD
{
    /// <summary>
    /// 提问数据访问类
    /// </summary>
    public class TopicDal
    {
        /// <summary>
        /// 新增提问信息
        /// </summary>
        /// <param name="topic"></param>
        /// <returns></returns>
        public int AddTopic(Topic topic)
        {
            string sql = "insert into tb_bbs_main(title,topiccontent,authorid,channelid,createtime,lasttime) values(@title,@topiccontent,@authorid,@channelid,getdate(),getdate())";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("title", System.Data.SqlDbType.VarChar, topic.title);
            tools.AddSqlParameter("topiccontent", System.Data.SqlDbType.VarChar, topic.topiccontent);
            tools.AddSqlParameter("authorid", System.Data.SqlDbType.Int, topic.authorid);
            tools.AddSqlParameter("channelid", System.Data.SqlDbType.Int, topic.channelid);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 获取提问信息
        /// </summary>
        /// <param name="topicID"></param>
        /// <returns></returns>
        public Topic GetTopic(int topicID)
        {
            string sql = "select * from tb_bbs_main where topicid=@topicid";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("topicid", System.Data.SqlDbType.Int, topicID);

            return SQLHelper.GetDataByColumns<Topic>(ConnectionManager.Emfstock, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 获取我的提问信息
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="channelid"></param>
        /// <returns></returns>
        public List<MyTopic> GetMyTopic(int userid, int channelid)
        {
            string sql = "select topicid,channelid from tb_bbs_main where authorid=@authorid and channelid=@channelid order by lasttime desc";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("authorid", System.Data.SqlDbType.Int, userid);
            tools.AddSqlParameter("channelid", System.Data.SqlDbType.Int, channelid);

            return SQLHelper.GetDataByColumnsLst<MyTopic>(ConnectionManager.Emfstock, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 删除提问信息
        /// </summary>
        /// <param name="topicid"></param>
        /// <returns></returns>
        public int DeleteTopic(int topicid)
        {
            string sql = "delete from tb_bbs_main where topicid=@topicid";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("topicid", System.Data.SqlDbType.Int, topicid);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
        }
    }
}
